// pages/face-pay/index.js
var wxdirectpay = require('../../utils/pay.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderAmount:0,//消费金额
    couponid: 0,
    money: 0,
    name: "",
    mydata:{},//选择优惠券数据
    creditLevel:{},
    credits:0,//积分
    cash:0,//余额支付
    checkChoose:null,
    mallName: "",
    cashInput:0,//输入余额
    payMoney:0//实际支付金额
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   let mallName = wx.getStorageSync('mallName');
   if (mallName){
     this.setData({
       mallName: mallName
     })
   }
   if (options.couponid){
     this.setData({
       couponid: options.couponid,
       money: options.money,
       name: options.name
     })
   }
   this.getStoreCreditLevel();
   this.getUserCash();
   this.getStoreInfo();
  },
  
  //获取店铺信息
  getStoreInfo:function(){
    var that = this;
    wx.request({
      url: app.globalData.host + "/api/Store/GetInfo?id=" + app.globalData.storeId,
      success: function(res) {
        if(res.data.Code == 0){
          if (res.data.Data.Logo){
           var Logo = "http://static.711688.net.cn/upload/store/" + app.globalData.storeId + "/logo/source/" + res.data.Data.Logo
          }
          that.setData({
            storeLogo: Logo
          })
        }
      },
    })
  },

  //获取消费金额
  GetorderAmount:function(e){
    if (e.detail.value){
      this.setData({
        orderAmount: e.detail.value
      })
    }
  },
  
  //获取积分店铺等级设置
  getStoreCreditLevel:function(){
    var that = this;
    wx.request({
      url: app.globalData.host + "/api/Store/GetStoreCreditConfigInfo?storeId=" + app.globalData.storeId,
      success: function(res) {
        console.log(res)
        if(res.data.Code == 0){
          that.setData({
            creditLevel: res.data.Data
          })
        }
      },
    })
  },
  
  //获取积分
  Getcredits:function(e){
    var that = this;
    if (e.detail.value){
      that.setData({
        credits: e.detail.value
      })
    }
  },
  
  //获取账户余额
  getUserCash: function () {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/UserCash/GetUserCash?uid=' + app.globalData.userInfo.Uid,
      success: function (res) {
        console.log(res)
        if (res.data.Code == 0) {
          if (res.data.Data.Cash>0){
            that.setData({
              cash: res.data.Data.Cash
            })
          }
        }
      }
    })
  },
  
  //获取余额支付
  getCashInput:function(e){
    var that = this;
    if (e.detail.value) {
      that.setData({
        cashInput: e.detail.value
      })
    }
  },
  

  //创建当面付订单
  paymentOrder:function(){
    var that = this;
    let creditLevel = that.data.creditLevel//积分
    let PayCreditPrice = creditLevel.PayCreditPrice//积分价格
    let cashNew = that.data.cashInput//余额
    let orderAmount = that.data.orderAmount//订单总计
    let credits = that.data.credits//支付积分
    let couponid = that.data.mydata.couponid ? that.data.mydata.couponid:""//客户已经激活的优惠劵
    let couponMoney = that.data.mydata.money ? that.data.mydata.money:0//优惠券金额
    if (orderAmount <= 0){
      wx.showModal({
        title: '提示',
        content: "请输入金额",
        showCancel: false
      })
      return;
    }
    if (cashNew > orderAmount) {//判断是否大于商品总额
      cashNew = orderAmount
    }
    let countMoney = parseFloat(credits / 100 * PayCreditPrice) + parseFloat(cashNew) + parseFloat(couponMoney)//优惠总和
    let payMoney = parseFloat(orderAmount) - parseFloat(countMoney)//实际付款
    if (payMoney >= 0 && payMoney < orderAmount){
      that.setData({
        payMoney: payMoney.toFixed(2)
      })
    }
    wx.request({
      url: app.globalData.host + "/api/Order/SubmitDirectPaymentOrder",
      data: {
        "Uid": app.globalData.userInfo.Uid,
        "StoreId": app.globalData.storeId,
        "OrderAmount": orderAmount,//订单总计
        "PayCreditCount": credits,//支付积分
        "CouponIdList": couponid, //客户已经激活的优惠劵
        "CouponSNList": "",//客户还未激活的优惠劵
        "PayCash": cashNew ? cashNew:0//余额支付
      },
      success: function(res) {
         wx.hideLoading();
         if (res.data.Code != 0) {
           wx.showModal({
             title: '提示',
             content: res.data.Message,
             showCancel: false
           })
           return;
         } else {
           if (countMoney >= orderAmount) {//判断优惠金额是否大于或等于订单金额
             wx.showToast({
               title: '支付成功',
               icon: 'success',
               duration: 2000
             })
             wx.redirectTo({
               url: '/pages/payFaceList/index',
             })
             return;
           } else {
             // 下单成功，跳转到支付界面
             let oid = res.data.Data;
             wxdirectpay.wxdirectpay(app, oid, "/pages/payFaceList/index");
             return;
           }
         }
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})